/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * StrategyEditorRenderer.java
 *
 * Created on 11/Dez/2011, 12:19:14
 */

package intopo.evogame.partnerselection.tools.config;

import intopo.evogame.partnerselection.config.TextConfigFilePlainPlayersEditor;
import intopo.game.Strategy;
import intopo.game.gui.AbstractStrategyEditorComponent;

import intopo.game.gui.ParsingException;
import java.awt.Color;
import java.awt.Component;
import java.util.EventObject;
import javax.swing.BorderFactory;
import javax.swing.JTable;
import javax.swing.event.CellEditorListener;
import javax.swing.event.EventListenerList;

/**
 * Represents the editor and renderer of a player's strategy.
 *
 * This class uses the description of a game to obtain the swing component that is used in a table.
 * 
 * @author <a href="mailto:mariano.pedro@gmail.com">Pedro Mariano</a>
 *
 * @version 1.0 11/Dez/2011
 */
class PlayerStrategyEditorRenderer
	extends javax.swing.AbstractCellEditor
	implements
		javax.swing.table.TableCellRenderer,
		javax.swing.table.TableCellEditor

{
	/**
	 * Component used to render and edit a strategy.
	 */
	final private AbstractStrategyEditorComponent compo;
	/**
	 * Listeners for this editor.
	 */
	final private EventListenerList eventListener;
	/**
	 * The player that is edited by the component.
	 */
	private TextConfigFilePlainPlayersEditor.Player player;

	PlayerStrategyEditorRenderer (AbstractStrategyEditorComponent compo)
	{
		this.compo = compo;
		this.eventListener = new EventListenerList ();
	}
	@Override
	public Component getTableCellRendererComponent (JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
	{
		this.player = (TextConfigFilePlainPlayersEditor.Player) value;
		this.compo.setStrategy (this.player.getStrategy ());
//		if (isSelected) {
//			this.compo.setBorder (BorderFactory.createEtchedBorder ());
//		}
//		else {
//			this.compo.setBorder (BorderFactory.createEmptyBorder ());
//		}
		if (isSelected) {
			this.compo.setBorder (BorderFactory.createLineBorder (Color.BLACK, (hasFocus ? 3 : 1)));
		}
		else {
			this.compo.setBorder (BorderFactory.createEmptyBorder ());
		}
		return this.compo;
	}

	@Override
	public Component getTableCellEditorComponent (JTable table, Object value, boolean isSelected, int row, int column)
	{
		this.player = (TextConfigFilePlainPlayersEditor.Player) value;
		this.compo.setStrategy (this.player.getStrategy ());
		if (isSelected) {
			this.compo.setBorder (BorderFactory.createEtchedBorder ());
		}
		else {
			this.compo.setBorder (BorderFactory.createEmptyBorder ());
		}
		return this.compo;
	}

	@Override
	public Object getCellEditorValue ()
	{
		try {
			Strategy s = this.compo.getStrategy ();
			System.out.println ("getCellEditorValue() = " + s);
			return s;
		}
		catch (ParsingException ex) {
			return null;
		}
	}

	@Override
	public boolean isCellEditable (EventObject anEvent)
	{
		return true;
	}

	@Override
	public boolean shouldSelectCell (EventObject anEvent)
	{
		return true;
	}

	/**
	 * We cannot stop editing a strategy.
	 * @return Always return {@code false}.
	 */
	@Override
	public boolean stopCellEditing ()
	{
		try {
			Strategy s = this.compo.getStrategy ();
			System.out.println ("stopCellEditing() = " + s);
			if (s != null) {
				this.player.setStrategy (s);
				this.fireEditingStopped ();
				return true;
			}
			else {
				return false;
			}
		}
		catch (ParsingException e) {
			return false;
		}
	}

	@Override
	public void cancelCellEditing ()
	{
		throw new UnsupportedOperationException ("Not supported yet.");
	}

	@Override
	public void addCellEditorListener (CellEditorListener l)
	{
		this.eventListener.add (CellEditorListener.class, l);
	}

	@Override
	public void removeCellEditorListener (CellEditorListener l)
	{
		this.eventListener.remove (CellEditorListener.class, l);
	}

}

// Local Variables:
// mode: java
// mode: flyspell-prog
// ispell-local-dictionary: "british"
// End:
